s = input().strip()
n = len(s)
prefix_A = [0 for _ in range(n + 1)]
prefix_B = [0 for _ in range(n + 1)]
for i in range(1, n + 1):
    prefix_A[i] = prefix_A[i - 1]
    prefix_B[i] = prefix_B[i - 1]
    if s[i - 1] == 'A':
        prefix_A[i] += 1
    else:
        prefix_B[i] += 1

result = 0
for j in range(n):
    # 定义末尾
    left = 0
    right = j
    ans = right + 1
    while left < right:
        mid = (left + right) // 2
        if prefix_A[j + 1] - prefix_A[mid] == prefix_B[j + 1] - prefix_B[mid]:
            ans = min(ans, mid)
            right = mid - 1
        else:
            left = mid + 1
    result = max(result, j - ans + 1)
print(result)
